package com.xjlsoft.weather.task;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjlsoft.weather.common.util.DataUtil;
import com.xjlsoft.weather.config.ApplicationContextProvider;
import com.xjlsoft.weather.entity.ExceptionLog;
import com.xjlsoft.weather.entity.QueryLog;
import com.xjlsoft.weather.entity.RequestApi;
import com.xjlsoft.weather.entity.WeatherTask;
import com.xjlsoft.weather.enums.RequestApiEnum;
import com.xjlsoft.weather.enums.WeatherTaskEnum;
import com.xjlsoft.weather.exception.WeatherException;
import com.xjlsoft.weather.mapper.ExceptionLogMapper;
import com.xjlsoft.weather.mapper.RequestApiMapper;
import com.xjlsoft.weather.mapper.WeatherTaskMapper;
import com.xjlsoft.weather.service.WeatherTaskJob;
import com.xjlsoft.weather.service.WeatherTaskService;

/**
 * 任务 01
 *
 * @author 码农猿
 */
public class GroundPreTaskTimin implements WeatherTaskJob {
	
	
	/**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(GroundPreTaskTimin.class);

    /**
     * 可重入锁
     */
    //private ReentrantLock lock = new ReentrantLock();
    
    private  WeatherTaskService weatherTaskService;
    
    
	private RequestApiMapper requestApiMapper;
	
	private WeatherTaskMapper  weatherTaskMapper;
    
	private ExceptionLogMapper exceptionLogMapper;
    /**
     * 处理地面数据  每天定时执行一次   
     * <br>
     * 完成业务逻辑:从启动时  抓取到  当日昨天的 23点的数据   例如今天是  2020-07-30 9::46   
     * <br/> 那数据抓取的日期为: [上次成功的日期 -至- 2020-07-29 23:00:00]
     * */
    @Override
    public void run() {
    	logger.info("######################开始 处理    数据           GroundPreTask（定时执行一次）   当前线程名称 {}", Thread.currentThread().getName());
    	
    	
    	//logger.info(">>>>>> 开始任务  加锁");f
    	//lock.lock();
    	requestApiMapper=ApplicationContextProvider.getBean(RequestApiMapper.class);
    	
    	weatherTaskMapper=ApplicationContextProvider.getBean(WeatherTaskMapper.class);
    	
    	exceptionLogMapper=ApplicationContextProvider.getBean(ExceptionLogMapper.class);

        RequestApi requestapi=requestApiMapper.selectById(RequestApiEnum.groundPre.getValue());
        
        WeatherTask wt=weatherTaskMapper.getByKey(WeatherTaskEnum.GroundPreTaskTimin.getValue());
       
        if(requestapi==null){
        	logger.error("未查询到RequestApi数据,request_api_id="+RequestApiEnum.groundPre.getValue());
        }
        try {

        	//logger.info(">>>>>>>>>>睡眠27秒>>>>>>>");
        	//Thread.currentThread();
			//Thread.sleep(1000*27);
        	
        	weatherTaskService=ApplicationContextProvider.getBean(WeatherTaskService.class);
        	
        	
        	QueryLog ql=weatherTaskService.createQuerylogTimin(requestapi,wt);
        	
        	if(ql!=null){
        		weatherTaskService.saveGroundPreJson(ql, requestapi,false);
        	}
        	
        	
		} catch (WeatherException we) {
			logger.error("创建   地面   数据    类型，请求数据异常",we);
			//保存失败请求的 原因  task_log表
			ExceptionLog ex=new ExceptionLog();
			ex.setExceptionId(IdWorker.getIdStr());
			ex.setCreateTime(DataUtil.getCurrenTimestamp());
			ex.setMessage(we.toString());
			StackTraceElement[] st=we.getStackTrace();
			if(st !=null){
				ex.setCodeStr(st[0].toString());
			}
			exceptionLogMapper.insert(ex);
		}  catch (Exception e) {
			e.printStackTrace();
		}
        logger.info("######################开始 处理    数据           GroundPreTask（定时执行一次）   当前线程名称 {}", Thread.currentThread().getName());
    }
}